package com.web.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.annotation.CurrentUser;
import com.web.domain.Response;
import com.web.entity.*;
import com.web.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * 用户收藏
 */
@Slf4j
@RestController
@RequestMapping(value = "/app")
public class UserCollectionController {


    @Autowired
    private UserCollectionService userCollectionService;
    @Autowired
    private HomeService homeService;
    @Autowired
    private SupplyService supplyService;
    @Autowired
    private AskQuestionsService askQuestionsService;
    @Autowired
    private JobService jobService;


    /**
     * 用户收藏
     * @param info
     * @param userCollection
     * @return
     */
    @PostMapping("/UserCollection/addUserCollection")
    public Response addUserCollection(@CurrentUser UserInfo info, UserCollection userCollection){
        if (StringUtils.isBlank(String.valueOf(userCollection.getTypeId())) ||
                StringUtils.isBlank(String.valueOf(userCollection.getType()))){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        UserCollection userCollection1 = userCollectionService.findCollection(info.getUserId(),userCollection.getTypeId(),userCollection.getType());
        if(userCollection1 == null){
            if(userCollection.getType()==8){
                UserQuiz userQuiz = askQuestionsService.findUserAskQuestions(userCollection.getTypeId());
                userQuiz.setCollection_num(userQuiz.getCollection_num()+1);
                askQuestionsService.updateCollectionNum(userQuiz);
                userCollection.setCollectionUserId(info.getUserId());
                userCollection.setType(userCollection.getType());
                userCollection.setTypeId(userCollection.getTypeId());
                userCollection.setCreateTime(new Date());
                userCollection.setModifyTime(new Date());
                userCollectionService.insertSelective(userCollection);
                return Response.custom().ok();
            }
            if(userCollection.getType()==3){
                FourIdleDetails userDetails = supplyService.findUserFourIdle(userCollection.getTypeId());
                userDetails.setCollection_num(userDetails.getCollection_num()+1);
                supplyService.updateCollectionNum(userDetails);
                userCollection.setCollectionUserId(info.getUserId());
                userCollection.setType(userCollection.getType());
                userCollection.setTypeId(userCollection.getTypeId());
                userCollection.setCreateTime(new Date());
                userCollection.setModifyTime(new Date());
                userCollectionService.insertSelective(userCollection);
                return Response.custom().ok();
            }
            if(userCollection.getType()==2){
                UserInstantLife userInstantLife = homeService.findUserDynamic(userCollection.getTypeId());
                userInstantLife.setCollection_num(userInstantLife.getCollection_num()+1);
                homeService.updateCollectionNum(userInstantLife);
                userCollection.setCollectionUserId(info.getUserId());
                userCollection.setType(userCollection.getType());
                userCollection.setTypeId(userCollection.getTypeId());
                userCollection.setCreateTime(new Date());
                userCollection.setModifyTime(new Date());
                userCollectionService.insertSelective(userCollection);
                return Response.custom().ok();
            }
            userCollection.setCollectionUserId(info.getUserId());
            userCollection.setType(userCollection.getType());
            userCollection.setTypeId(userCollection.getTypeId());
            userCollection.setCreateTime(new Date());
            userCollection.setModifyTime(new Date());
            userCollectionService.insertSelective(userCollection);
            return Response.custom().ok();
        }
       return Response.custom().failure("您已收藏过该条内容，请勿重复操作！");
    }


    /**
     * 取消收藏
     * @param info
     * @param
     * @return
     */
    @PostMapping("/UserCollection/CancelUserCollection")
    public Response CancelUserCollection(@CurrentUser UserInfo info, Integer type,Integer typeId){
        if (type == null || typeId == null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        UserCollection userCollection = userCollectionService.findCollection(info.getUserId(),typeId,type);
        if(userCollection != null){
            userCollectionService.deleteUserCollection(info.getUserId(),typeId,type);
            return Response.custom().ok();
        }
        return Response.custom().failure("您还没有收藏该条内容无法取消！");
    }

    /**
     * 我的收藏
     * @param info
     * @return
     */
    @PostMapping("/UserCollection/MyUserCollection")
    public Response MyUserCollection(@CurrentUser UserInfo info, Integer pageNum) {
        if(pageNum==null){
            return Response.custom().failure("缺少页码！");
        }
        PageHelper.startPage(pageNum, 10);
        List<InstantInfoCollection> list = userCollectionService.findMyCollection(info.getUserId());
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return Response.custom().ok().addParams(new List[0]);
        }else{
            return Response.custom().ok().addParams(pageInfo.getList());
        }
    }



}
